package cz.cuni.mff.ufal.volk.langr;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URL;

import cz.cuni.mff.ufal.volk.Text;
import cz.cuni.mff.ufal.volk.services.LanguageRecognizer;

public class ResourceLanguageRecognizer implements LanguageRecognizer<Text> {

  public ResourceLanguageRecognizer(String name) throws IOException, ClassNotFoundException {
		this(name, ResourceLanguageRecognizer.class);
	}

	@SuppressWarnings("unchecked")
  public ResourceLanguageRecognizer(String name, Class<?> t) throws IOException, ClassNotFoundException {
		URL inResource = ResourceLanguageRecognizer.class.getResource("/model/" + name);
		InputStream inStream = inResource.openStream();
		try {
			ObjectInputStream in = new ObjectInputStream(inStream);
			try {
				underlying = (LanguageRecognizer<Text>)in.readObject();
			} finally {
				in.close();
			}
		} finally {
			inStream.close();
		}
	}

	private final LanguageRecognizer<Text> underlying;

	@Override
  public String recognize(Text item) {
	  return underlying.recognize(item);
  }

}
